package cn.com.generaldata.os.paging;

import java.util.List;

import cn.com.generaldata.os.util.PageUtils;

public class Result {

	private List<?> operationResult;
	private List<Event> events;
	private int pageSize;
	private int pageNumber;
	private int totalCount;

	public Result() {
	}

	public Result(int pageNumber, int pageSize) {
		this.pageSize = pageSize;
		this.pageNumber = pageNumber;
	}

	public Result(int pageNumber, int pageSize, int totalCount, List<?> operationResult, List<Event> events) {
		this.totalCount = 0;
		if (pageSize <= 0) {
			throw new IllegalArgumentException("[pageSize] must great than zero");
		} else {
			this.pageSize = pageSize;
			this.pageNumber = PageUtils.computePageNumber(pageNumber, pageSize, totalCount);
			this.totalCount = totalCount;
			setOperationResult(operationResult);
			this.events = events;
			return;
		}
	}

	public void setOperationResult(List<?> operationResult) {
		if (operationResult == null) {
			throw new IllegalArgumentException("'operationResult' must be not null");
		} else {
			this.operationResult = operationResult;
			return;
		}
	}

	public List<?> getOperationResult() {
		return operationResult;
	}

	public List<Event> getEvents() {
		return events;
	}

	public void setEvents(List<Event> events) {
		this.events = events;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getThisPageNumber() {
		return pageNumber;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getFirstResult() {
		return PageUtils.getFirstResult(pageNumber, pageSize);
	}

	public boolean isFirstPage() {
		return getThisPageNumber() == 1;
	}

	public boolean isLastPage() {
		return getThisPageNumber() >= getLastPageNumber();
	}

	public boolean isHasNextPage() {
		return getLastPageNumber() > getThisPageNumber();
	}

	public boolean isHasPreviousPage() {
		return getThisPageNumber() > 1;
	}

	public int getLastPageNumber() {
		return PageUtils.computeLastPageNumber(totalCount, pageSize);
	}

	public int getThisPageFirstElementNumber() {
		return (getThisPageNumber() - 1) * getPageSize() + 1;
	}

	public int getThisPageLastElementNumber() {
		int fullPage = (getThisPageFirstElementNumber() + getPageSize()) - 1;
		return getTotalCount() >= fullPage ? fullPage : getTotalCount();
	}

	public int getNextPageNumber() {
		return getThisPageNumber() + 1;
	}

	public int getPreviousPageNumber() {
		return getThisPageNumber() - 1;
	}
}
